package com.yzfarm.biujoke.biujoke.mvp.view;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.yzfarm.biujoke.biujoke.R;
import com.yzfarm.biujoke.biujoke.mvp.base.BaseFragment;
import com.yzfarm.biujoke.biujoke.mvp.model.bean.Contentlist;
import com.yzfarm.biujoke.biujoke.mvp.presenter.JokePresenter;
import com.yzfarm.biujoke.biujoke.ui.adapter.JokeRecyclerAdpter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hanhy on 2016/10/9.
 */

public class JokeFragment extends BaseFragment<JokePresenter> {

    private RecyclerView recyclerView;
    private JokeRecyclerAdpter adpter;
    private GridLayoutManager gridLayoutManager;
    private List<Contentlist> mlist;
    private int page = 1;


    @Override
    protected void initInject() {

        mPresenter = new JokePresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.joke_layout;
    }

    @Override
    protected void initEventAndData() {

        mlist = new ArrayList<Contentlist>();
        recyclerView = (RecyclerView) getView().findViewById(R.id.joke_content);
        gridLayoutManager = new GridLayoutManager(getContext(),1);
        recyclerView.setLayoutManager(gridLayoutManager);
        adpter = new JokeRecyclerAdpter(getContext(),mlist);
        recyclerView.setAdapter(adpter);
        mPresenter.getJoke(page+"");
        recyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(View view) {
                int position = (Integer) view.getTag(R.integer.joke_id);
                if(position == mlist.size() -1){
                    page++;
                    mPresenter.getJoke(page+"");
                }

            }

            @Override
            public void onChildViewDetachedFromWindow(View view) {

            }
        });

    }

    @Override
    public void showError(String msg) {

    }

    public void showJoke(List<Contentlist> list){
        mlist.addAll(list);
        adpter.setmData(mlist);
        adpter.notifyDataSetChanged();
    }
}
